from api.service.rule_storage_service import LocalStorage

def test_storage():
    storage = LocalStorage()

    # 添加数据
    storage.add_dict("cat_001", "P001", {"product": "手机", "price": 5999})
    storage.add_dict("cat_001", "P002", {"product": "平板", "price": 3999})
    storage.add_dict("cat_001", "P003", {"product": "耳机", "price": 299})

    storage.add_dict("cat_002", "B001", {"book": "Python编程", "author": "Guido"})
    storage.add_dict("cat_002", "B002", {"book": "算法导论", "author": "Cormen"})

    print("\n--- 当前结构 ---")
    print("类别 -> 编码:", storage.list_all_categories())
    print("编码 -> 数据:", storage.list_all_data())

    print("\n--- 查询类别 cat_001 的所有数据 ---")
    print(storage.get_dicts("cat_001"))

    print("\n--- 删除 product 为 '平板' 的条目 ---")
    try:
        storage.remove_by_field("cat_001", "product", "平板")
    except KeyError as e:
        print(f"删除失败: {e}")

    print("\n--- 删除编码 P001 ---")
    storage.remove_by_code("P001")

    print("\n--- 删除类别 cat_002 ---")
    storage.remove_by_category("cat_002")

    print("\n--- 最终状态 ---")
    print("类别 -> 编码:", storage.list_all_categories())
    print("编码 -> 数据:", storage.list_all_data())
